import { defineStore } from "pinia";
import { ref } from "vue";

const useUserStore = defineStore("user", ()=> {
  const userInfo = ref({});
  const loginStatus = ref(false);
  const token = ref("");

  /**
   * Login
   * @returns 
   */
  function handlerLogin() {
    return new Promise((resolve, reject)=> {
      const num = Math.random();
      if(num > 0.8) {
        loginStatus.value  = true;
        token.value = String(new Date().getTime());
        userInfo.value = {
          name: "admin",
          avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80"
        };
        resolve("Login success");
      } else {
        reject("Login field");
      }
    });
  }

  /**
   * Logout
   */
  function handlerLogout() {
    return new Promise((resolve)=> {
      loginStatus.value = false;
      token.value = "";
      userInfo.value = {};
      resolve();
    });
  }

  return {
    userInfo,
    loginStatus,
    token,
    handlerLogin,
    handlerLogout,
  };
});


export default useUserStore;